package handler

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"user_admin/global"
	"user_admin/internal/request"
	"user_admin/models"
	"user_admin/pkg"
)

func ChatAdd(c *gin.Context) {
	var chat request.ChatAdd
	if err := c.ShouldBind(&chat); err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "获取失败",
			"data": nil,
		})
		return
	}

	var chats models.Context
	err := global.DB.Create(&chats).Error
	simple := pkg.NewRabbitMQSimple("chat_send")
	publishSimple := simple.PublishSimple(chat.Content)
	marshal, _ := json.Marshal(publishSimple)
	pkg.NewRabbitMQSimple(string(marshal))
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "评论失败",
			"data": nil,
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "评论成功",
		"data": chat,
	})

}
